﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Freshome.m.vws
{
    /// <summary>
    /// UserInfox.xaml 的交互逻辑
    /// </summary>
    public partial class UserInfox : Window
    {
        public UserInfox()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        private fox.Userole m_userole = null;
        public fox.Userole Userole
        {
            get
            {
                return m_userole;
            }
        }
        private vms.Magicommand _fetch;
        private void FetchInfo()
        {
            bool gotten = false;
            try
            {
                m_userole = new fox.Userole
                {
                    UserName = this.userName.Text,
                    UserPass = this.userPass.Password
                };
                fox.Helproxy<Rpb.IPublicall> helper = new fox.Helproxy<Rpb.IPublicall>(m_userole);
                helper.Syncall(cln =>
                {
                    m_userole.Workerole = cln.MyRolentry();
                    m_userole.IsSenior = (m_userole.Workerole & Rpb.Workerole.Senior) == Rpb.Workerole.Senior;
                    m_userole.IsJunior = (m_userole.Workerole & Rpb.Workerole.Junior) == Rpb.Workerole.Junior;
                    gotten = true;
                }, new Rpb.PublicallClient("wsHttp_IPublicall"));
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message, "无效调用");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "本地异常");
            }
            DialogResult = gotten;
        }
        private bool Fetchable()
        {
            return this.userName.Text != null && this.userName.Text.Length > 1 && this.userPass.Password != null && this.userPass.Password.Length > 1;
        }
        public vms.Magicommand Fetch
        {
            get
            {
                if (_fetch == null) _fetch = new vms.Magicommand("确定", param => this.FetchInfo(), param => this.Fetchable());
                return _fetch;
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.userName.Focus();
        }
    }
}
